import {createRouter, createWebHistory} from "vue-router";
import Login from "../views/Login.vue";
import Layout from "../views/Layout.vue";
import UserInfo from "../views/user/UserInfo.vue";
import RestPassword from "../views/user/RestPassword.vue";
import { useUserInfoStore } from '@/stores/userInfo'

// 定义路由关系
const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/property/statistics',
    children: [
      {
        path: '/property/base',
        component: () => import('../views/property/PropertyBase.vue'),
        meta: { permissions: ['property_infra'] }
      },
      {
        path: '/property/statistics',
        component: () => import('../views/property/PropertyStatistics.vue'),
        meta: { permissions: ['property_infra', 'property_allocation'] }
      },
      {
        path: '/property/building',
        component: () => import('../views/property/PropertyBuilding.vue'),
        meta: { permissions: ['property_infra'] }
      },
      {
        path: '/property/bed-management',
        component: () => import('../views/property/PropertyBedManagement.vue'),
        meta: { permissions: ['property_infra'] }
      },
      {
        path: '/property/allocation',
        component: () => import('../views/property/allocation/PropertyAllocationMove.vue'),
        meta: { permissions: ['property_allocation'] }
      },
      {
        path: '/property/allocation/undergraduate',
        component: () => import('../views/property/allocation/PropertyAllocationUndergraduate.vue'),
        meta: { permissions: ['alloc_undergrad'] }
      },
      {
        path: '/property/allocation/graduate',
        component: () => import('../views/property/allocation/PropertyAllocationGraduate.vue'),
        meta: { permissions: ['alloc_grad'] }
      },
      {
        path: '/property/allocation/faculty',
        component: () => import('../views/property/allocation/PropertyAllocationFaculty.vue'),
        meta: { permissions: ['alloc_faculty'] }
      },
      {
        path: '/system/permissions',
        name: 'system-permissions',
        component: () => import('../views/system/SystemPermissions.vue'),
        meta: { permissions: ['system_permissions'] }
      },
      {
        path: '/user/info',
        name: 'user-info',
        component: () => import('../views/user/UserInfo.vue'),
        meta: { title: '基本资料', icon: 'User' }
      },
      {
        path: '/user/reset-password',
        name: 'reset-password',
        component: () => import('../views/user/RestPassword.vue'),
        meta: { title: '重置密码', icon: 'EditPen' }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
]
// 创建路由器
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const userInfoStore = useUserInfoStore()
  const token = userInfoStore.token
  const userPermissions = userInfoStore.userInfo.permissions || []

  if (to.name !== 'login' && !token) {
    // 如果用户未登录且访问的不是登录页，则重定向到登录页
    next({ name: 'login' })
  } else if (to.meta.permissions) {
    // 如果路由需要权限
    const requiredPermissions = to.meta.permissions as string[]
    const hasPermission = requiredPermissions.some((p) => userPermissions.includes(p))

    if (hasPermission) {
      next() // 有权限，放行
    } else {
      // 无权限，可以重定向到403页面或首页
      // 这里我们简单地阻止导航，或者可以 next({ path: '/403' })
      next(from.path) // 或者返回上一页
    }
  } else {
    // 不需要权限的路由或登录页，直接放行
    next()
  }
})

//导出暴露
export default router
